package com.Dish;

import java.util.ArrayList;
import java.util.Scanner;

public class DishManager {
    private ArrayList<Dish> dishes = new ArrayList<>();

    public DishManager(){
        dishes.add(new Dish("001", "小葱拌豆腐", 100, 10, "素菜", "有点香", new String[]{"小葱", "豆腐", "秘制料汁"}));
        dishes.add(new Dish("002", "红烧茄子", 20, 19.9, "素菜", "有点骚", new String[]{"茄子", "蒜", "酱油", "姜"}));
    }
    /**
     * 作用: 根据编号菜品的索引
     * @param id 菜品的编号
     * @return 菜品在集合中的索引
     */
    public  int getDishIndexById( String id) {
        for (int i = 0; i < dishes.size(); i++) {
            Dish dish = dishes.get(i);
            if (dish.getId().equals(id)) {
                return i;
            }
        }
        return -1;


    }

    /**
     * 修改菜品
     */
    public  void updateDish() {
        Scanner scanner = new Scanner(System.in);
        //请输入修改的编号
        //键盘将所有的菜品都输入进去
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要修改的菜的编号");
        String id = sc.next();
        //有可能不存在
        int index = getDishIndexById( id);
        if (index == -1) {
            System.out.println("修改的id不存在, 你妹的~~~");
            return;
        }

        System.out.println("请输入新的菜名字,-1不修改");
        String name = sc.next();
        if (!name.equals("-1")) {
            dishes.get(index).setName(name);
        }

        System.out.println("请输入要修改菜的原价,-1代表不修改");
        double price = sc.nextDouble();
        if (price != -1) {
            dishes.get(index).setPrice(price);
        }

        System.out.println("请输入修改的菜的打折价,-1代表不修改");
        double discountPrice = sc.nextDouble();
        if (discountPrice != -1) {
            dishes.get(index).setDiscountPrice(discountPrice);
        }

        System.out.println("请输入菜新的标签,-1代表不修改");
        String label = sc.next();
        if (!label.equals("-1")) {
            dishes.get(index).setLabel(label);
        }

        System.out.println("请输入菜的口味,-1代表不修改");
        String taste = sc.next();
        if (!taste.equals("-1")) {
            dishes.get(index).setTaste(taste);
        }
        System.out.println("请输入菜的材料,多个材料之间使用逗号隔开,-1代表不修改");
        String foodsStr = sc.next();//"龙虾,姜,大葱,辣椒"
        if (!foodsStr.equals("-1")) {
            String[] foods = foodsStr.split(",");
            dishes.get(index).setFoods(foods);
        }
        System.out.println("修改成功");
    }

    /**
     * 作用: 根据价格查询菜品
     *
     * @param name   菜名
     */
    public  void showDishByName( String name) {
        if (dishes.size() == 0) {
            System.out.println("没有菜品可以查询~~~");
            return;
        }
        int count = 0;
        for (int i = 0; i < dishes.size(); i++) {
            Dish dish = dishes.get(i);
            if (dish.getName().contains(name)) {
                count++;
                System.out.println("菜品的编号:" + dish.getId());
                System.out.println("菜品的名字:" + dish.getName());
                System.out.println("菜品的原价:" + dish.getPrice());
                System.out.println("菜品的折扣价:" + dish.getDiscountPrice());
                System.out.print("菜品的原料:");
                String[] foods = dish.getFoods();
                for (int j = 0; j < foods.length; j++) {
                    System.out.print(foods[j] + " ");
                }
                System.out.println();
                System.out.println("菜品的标签:" + dish.getLabel());
                System.out.println("菜品的口味:" + dish.getTaste());
                System.out.println("==============================");
            }
        }
        if (count == 0) {
            System.out.println("没有该名字的菜品~~~");
        }

    }

    /**
     * 作用: 根据价格查询菜品
     *
     * @param lowPrice  低价
     * @param highPrice 高价
     */
    public  void showDishByPrice( double lowPrice, double highPrice) {
        if (dishes.size() == 0) {
            System.out.println("没有菜品可以查询~~~");
            return;
        }
        int count = 0;
        for (int i = 0; i < dishes.size(); i++) {
            Dish dish = dishes.get(i);
            if (dish.getPrice() >= lowPrice && dish.getPrice() <= highPrice) {
                count++;
                System.out.println("菜品的编号:" + dish.getId());
                System.out.println("菜品的名字:" + dish.getName());
                System.out.println("菜品的原价:" + dish.getPrice());
                System.out.println("菜品的折扣价:" + dish.getDiscountPrice());
                System.out.print("菜品的原料:");
                String[] foods = dish.getFoods();
                for (int j = 0; j < foods.length; j++) {
                    System.out.print(foods[j] + " ");
                }
                System.out.println();
                System.out.println("菜品的标签:" + dish.getLabel());
                System.out.println("菜品的口味:" + dish.getTaste());
                System.out.println("==============================");
            }
        }
        if (count == 0) {
            System.out.println("你查询的价格区间没有菜品~~~");
        }

    }

    /**
     * 作用: 展示所有菜品
     *
     */
    public  void showAllDishes() {
        if (dishes.size() == 0) {
            System.out.println("没有菜品可以查询~~~");
            return;
        }
        for (int i = 0; i < dishes.size(); i++) {
            Dish dish = dishes.get(i);
            System.out.println("菜品的编号:" + dish.getId());
            System.out.println("菜品的名字:" + dish.getName());
            System.out.println("菜品的原价:" + dish.getPrice());
            System.out.println("菜品的折扣价:" + dish.getDiscountPrice());
            System.out.print("菜品的原料:");
            String[] foods = dish.getFoods();
            for (int j = 0; j < foods.length; j++) {
                System.out.print(foods[j] + " ");
            }
            System.out.println();
            System.out.println("菜品的标签:" + dish.getLabel());
            System.out.println("菜品的口味:" + dish.getTaste());
            System.out.println("==============================");
        }

    }


    /**
     * 作用: 根据编号下架菜品
     *
     * @param id     传入的编号
     */
    public  void removeDishById( String id) {
        //遍历集合,取出每一个菜品对象, 然后通过菜品对象, 取出其id, 和要删除的id对比,一致的话则删除, 如果输入的id不存在你要提示
        int index = -1;
        for (int i = 0; i < dishes.size(); i++) {
            Dish dish = dishes.get(i);
            if (dish.getId().equals(id)) {
                index = i;
                break;
            }
        }
        //判断index是否还是-1
        if (index == -1) {
            //没查到
            System.out.println("您要删除的id不存在, 删除失败");
        } else {
            dishes.remove(index);
            System.out.println("删除成功");
        }
    }

    /**
     * 作用: 添加菜品
     */
    public  void addDish() {

        System.out.println("欢迎进入添加菜品, 请输入菜品的信息");
        //键盘将所有的菜品都输入进去
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入菜的编号");
        String id = sc.next();

        System.out.println("请输入菜的名字");
        String name = sc.next();

        System.out.println("请输入菜的原价");
        double price = sc.nextDouble();

        System.out.println("请输入菜的打折价");
        double discountPrice = sc.nextDouble();

        System.out.println("请输入菜的标签");
        String label = sc.next();

        System.out.println("请输入菜的口味");
        String taste = sc.next();

        System.out.println("请输入菜的材料,多个材料之间使用逗号隔开");
        String foodsStr = sc.next();//"龙虾,姜,大葱,辣椒"
        String[] foods = foodsStr.split(",");
        //将零散的数据封装成Dish对象, 然后在将Dish对象放入集合中
        Dish dish = new Dish(id, name, price, discountPrice, label, taste, foods);

        //将菜品添加到集合中
        dishes.add(dish);

    }

    /**
     * 作用: 展示菜品的菜单
     */
    public void showMenu(){

        Scanner sc = new Scanner(System.in);
        while (true) {
            //2.打印菜单
            System.out.println("-----欢迎来到吃不了兜着走餐厅------");
            System.out.println("请选择你的操作");
            System.out.println("1.上架菜品");
            System.out.println("2.下架菜品");
            System.out.println("3.展示全部菜品");
            System.out.println("4.根据价格查询菜品");
            System.out.println("5.根据名字查询菜品");
            System.out.println("6.根据编号查询菜品");
            System.out.println("7.修改菜品");
            System.out.println("8.退出系统");
            int choose = sc.nextInt();
            switch (choose) {
                case 1 -> {
                    addDish();
                }
                case 2 -> {
                    System.out.println("请输入要删除菜品的编号");
                    String id = sc.next();
                    removeDishById( id);

                }
                case 3 -> {
                    showAllDishes();
                }
                case 4 -> {
                    System.out.println("请输入低价");
                    double lowPrice = sc.nextDouble();
                    System.out.println("请输入高价");
                    double highPrice = sc.nextDouble();
                    showDishByPrice( lowPrice, highPrice);
                }
                case 5 -> {
                    System.out.println("请输入要查询菜品的名字");
                    String name = sc.next();
                    showDishByName( name);
                }
                case 6 -> {

                }
                case 7 -> {
                    updateDish();
                }
                case 8 -> {
                    System.out.println("不要再来了~~~在来揍你~~~");
                    return;
                }
            }
        }
    }
}
